﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Services;

using Domain;

namespace WebRole1
{
	public partial class ViewSubforum : System.Web.UI.Page
	{
		private void Page_Init(object sender, System.EventArgs e)
		{
			this.EnableViewState = false;
		}



		/// <summary>
		/// 
		/// </summary>
		public System.Drawing.Color Color0
		{
			get
			{
				return System.Drawing.Color.FromArgb(180, 180, 180);
			}
		}

		public System.Drawing.Color Color1
		{
			get
			{
				return System.Drawing.Color.White;
			}
		}

		public System.Drawing.Color Color2
		{
			get
			{
				return System.Drawing.Color.FromArgb(238, 238, 238);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			Title = "Forum - Index";

			HtmlMeta metaDesc = new HtmlMeta();
			metaDesc.Name = "description";
			metaDesc.Content = "forum jongeren";
			Page.Header.Controls.Add(metaDesc);

			HtmlMeta metaKey = new HtmlMeta();
			metaKey.Name = "keywords";
			metaKey.Content = "forum jongeren";
			Page.Header.Controls.Add(metaKey);



			decimal start = System.DateTime.Now.Ticks;

			Int16 subforumID = 0;

			if (HttpContext.Current.Request.QueryString["id"] != null)
			{
				subforumID = Int16.Parse(HttpContext.Current.Request.QueryString["id"].ToString());
			}


			hlNewTopic.NavigateUrl = "~/NewTopic.aspx?subforum=" + subforumID;




			Int64 forumID = Int64.Parse(HttpContext.Current.Request.QueryString["id"].ToString());


			TopicService topicServ = new TopicService();
			IList<Topic> topics = topicServ.getTopics(forumID);

			int backcolorType = 0;

			TableRow tr = NewTableRow(backcolorType);
			tr.Width = Unit.Pixel(700);


			//Forumname
			TableCell tc = NewTableCell();
			tc.Width = Unit.Pixel(400);
			Label lblTopicName = new Label();
			lblTopicName.Text = "Topic";
			tc.Controls.Add(lblTopicName);
			tr.Cells.Add(tc);

			//Topicstarter
			tc = NewTableCell();
			tc.Width = Unit.Pixel(150);
			Label lblTopicStarter = new Label();
			lblTopicStarter.Text = "Topicstarter";
			tc.Controls.Add(lblTopicStarter);
			tr.Cells.Add(tc);



			//Last poster
			tc = NewTableCell();
			tc.Width = Unit.Pixel(200);
			Label lblLastPoster = new Label();
			lblLastPoster.Text = "Last post";
			tc.Controls.Add(lblLastPoster);
			tr.Cells.Add(tc);


			//Posts
			tc = NewTableCell();
			tc.Width = Unit.Pixel(45);
			Label lblPostcount = new Label();
			lblPostcount.Text = "Posts";
			tc.Controls.Add(lblPostcount);
			tr.Cells.Add(tc);

			//Views
			tc = NewTableCell();
			tc.Width = Unit.Pixel(45);
			Label lblViewcount = new Label();
			lblViewcount.Text = "Views";
			tc.Controls.Add(lblViewcount);
			tr.Cells.Add(tc);

			tblTopics.Rows.Add(tr);





			backcolorType = 1;

			foreach(Topic t in topics)
			{
				lblSubforum.Text = t.SubforumName;

				tr = NewTableRow(backcolorType);
				tr.Width = Unit.Pixel(700);

				//Forumname
				tc = NewTableCell();
				HyperLink hlTopicName = new HyperLink();
				hlTopicName.NavigateUrl = "~/topic/" + t.ID.ToString() + "/1/" + t.UrlTitle;

				if (t.Title.Length >= 65)
				{
					hlTopicName.Text = t.Title.Substring(0, 65) + "...";
				}
				else
				{
					hlTopicName.Text = t.Title;
				}
				hlTopicName.ID = t.Title;

				tc.Controls.Add(hlTopicName);
				tr.Cells.Add(tc);



				int i = 2;
				Int64 pagecount = ((t.Posts - 1) / 25) + 1;
				while (i <= pagecount)
				{
					Label lbl = new Label();
					lbl.Text = " ";
					tc.Controls.Add(lbl);

					HyperLink hlPage = new HyperLink();
					hlPage.NavigateUrl = "~/topic/" + t.ID.ToString() + "/" + i.ToString() + "/" + t.UrlTitle;
					hlPage.Text = i.ToString();
					tc.Controls.Add(hlPage);
					i++;
				}






				//Topicstarter
				string TopicStarterName = "";
				tc = NewTableCell();
				

				int length = 0;
				if (t.TopicStarterName.Length > 15)
				{
					length = 15;
				}
				else
				{
					length = t.TopicStarterName.Length;
				}

				if (t.TopicStarterName.Length > 0)
				{
					TopicStarterName = t.TopicStarterName.Substring(0, length);
				}

				if (t.TopicStarterID != "")
				{
					HyperLink hlTopicStarter = new HyperLink();
					hlTopicStarter.NavigateUrl = "./Profile.aspx?user=" + t.TopicStarterName;

					hlTopicStarter.Text = TopicStarterName;
					tc.Controls.Add(hlTopicStarter);

					tr.Cells.Add(tc);
				}
				else
				{
					Label hlTopicStarter = new Label();

					hlTopicStarter.Text = TopicStarterName;
					tc.Controls.Add(hlTopicStarter);

					tr.Cells.Add(tc);
				}

				tr.Cells.Add(tc);





				//Last poster
				string LastPosterName = "";

				tc = NewTableCell();
				Label lblLastPostOn = new Label();
				lblLastPostOn.Text = t.LastPostOn + " - ";
				tc.Controls.Add(lblLastPostOn);

				length = 0;
				if (t.LastPosterName.Length > 15)
				{
					length = 15;
				}
				else
				{
					length = t.LastPosterName.Length;
				}

				if (t.LastPosterName.Length > 0)
				{
					LastPosterName = t.LastPosterName.Substring(0, length);
				}


				if (t.LastPosterID != "")
				{
					HyperLink hlLastPoster = new HyperLink();
					hlLastPoster.NavigateUrl = "./Profile.aspx?user=" + t.LastPosterName;

					hlLastPoster.Text = LastPosterName;
					tc.Controls.Add(hlLastPoster);

					tr.Cells.Add(tc);
				}
				else
				{
					Label hlLastPoster = new Label();

					hlLastPoster.Text = LastPosterName;
					tc.Controls.Add(hlLastPoster);

					tr.Cells.Add(tc);
				}


				tr.Cells.Add(tc);

				//Posts
				tc = NewTableCell();
				tc.BackColor = Color2;
				lblPostcount = new Label();
				lblPostcount.Text = t.Posts.ToString();
				tc.Controls.Add(lblPostcount);
				tr.Cells.Add(tc);

				//Views
				tc = NewTableCell();
				tc.BackColor = Color1;
				lblViewcount = new Label();
				lblViewcount.Text = t.Views.ToString();
				tc.Controls.Add(lblViewcount);
				tr.Cells.Add(tc);




				tblTopics.Rows.Add(tr);
			}

			decimal end = System.DateTime.Now.Ticks;
			lblPageloadDuration.Text = "page loaded in " + ((end - start) / 10000000).ToString("n3") + " seconds";
		}




		private Table NewTable()
		{
			Table tbl = new Table();
			tbl.CellPadding = 0;
			tbl.CellSpacing = 0;

			return tbl;
		}

		private TableRow NewTableRow(int backcolorType)
		{
			TableRow row = new TableRow();
			row.BorderStyle = BorderStyle.None;
			row.Width = Unit.Pixel(700);
			row.HorizontalAlign = HorizontalAlign.Justify;


			if (backcolorType == 0)
			{
				//Header
				row.BackColor = Color0;
				row.BorderColor = Color0;

			}
			if (backcolorType == 1)
			{
				//Datarow1
				row.BackColor = Color1;
				row.BorderColor = Color1;

			}
			else if (backcolorType == 2)
			{
				//Datarow2
				row.BackColor = Color2;
				row.BorderColor = Color2;
			}

			return row;
		}

		private TableCell NewTableCell()
		{
			TableCell cell = new TableCell();
			cell.BorderStyle = BorderStyle.None;
			return cell;
		}

	}
}